﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;

using mvcForum.Core;
using mvcForum.Core.Abstractions.Interfaces;

using mvcForum.Web.Extensions;
using mvcForum.Core.Configuration;

namespace mvcForum.Web.ViewModels {

	public class MessageViewModel : ForumViewModelBase {

		public MessageViewModel(Post message, AdminAuthorNameSetting authorSetting) {
			this.LastRead = false;

			this.Id = message.Id;
			this.Subject = message.Subject;
			this.Body = message.Body;
			this.Author = message.AuthorName;
			this.AuthorId = message.Author.Id;

			ForumUser author = message.Author;
			this.Joined = author.FirstVisit;
			//this.Joined = author.Created;
			this.PrettyName = this.Author;
			// TODO: Should we do this? Or should we keep the data updated ?!
			this.PrettyName = author.AuthorName(authorSetting);

			this.Posted = message.Posted;
			this.Position = message.Position;
			this.Flag = message.Flag;

			this.TopicId = message.Topic.Id;
			this.TopicLocked = ((message.Topic.Flag & TopicFlag.Locked) != 0);

			this.Access = message.Topic.Forum.GetAccess();

			this.LastEdited = message.Edited;
			this.ModeratorChanged = message.ModeratorChanged;
			this.DeleteReason = message.DeleteReason;

			this.CustomProperties = message.GetCustomProperties();
		}

		#region Access properties
		private AccessFlag Access { get; set; }
		public bool IsModerator {
			get {
				return this.HasAccess(AccessFlag.Moderator);
			}
		}

		public bool CanUpload {
			get {
				return this.HasAccess(AccessFlag.Upload);
			}
		}

		public bool CanPost {
			get {
				return this.HasAccess(AccessFlag.Post);
			}
		}

		public bool CanDelete {
			get {
				return this.HasAccess(AccessFlag.Delete);
			}
		}

		public bool CanEdit {
			get {
				return (
						(this.HasAccess(AccessFlag.Edit) && (this.Authenticated && this.CurrentUser.Id == this.AuthorId))
					);
			}
		}

		private bool HasAccess(AccessFlag flag) {
			return (this.Access & flag) == flag;
		}
		#endregion

		public Int32 Id { get; private set; }
		public String Subject { get; private set; }
		public String Body { get; private set; }
		public Int32 AuthorId { get; private set; }
		public String Author { get; private set; }
		public DateTimeOffset Joined { get; private set; }
		public DateTimeOffset Posted { get; private set; }
		public Int32 Position { get; private set; }
		public DateTimeOffset? LastEdited { get; private set; }
		public Boolean ModeratorChanged { get; private set; }
		public String PrettyName { get; private set; }
		public PostFlag Flag { get; private set; }
		public String DeleteReason { get; private set; }

		public Boolean TopicLocked { get; private set; }
		public Int32 TopicId { get; private set; }
		public TopicViewModel Topic { get; set; }
		public IEnumerable<AttachmentViewModel> Attachments { get; set; }
		public Boolean LastRead { get; set; }

		public IDictionary<String, String> CustomProperties { get; private set; }
	}
}